package com.yiran.dao;

import java.sql.*;
import java.util.*;

import com.yiran.model.PriceClass;
import com.yiran.util.DBConnection;

public class PriceBean {
	private Connection con;
	PriceClass price = new PriceClass();

	public PriceBean() {
		try {
			con = DBConnection.getConnection();
			System.out.println("conn: " + con);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setPriceInfo(PriceClass price) {
		this.price = price;
	}

	public void addPriceInfo() throws Exception {
		try {

			PreparedStatement stm = con
					.prepareStatement("insert into price(scopes) values(?)");
			stm.setString(1, price.getScope());

			try {
				stm.executeUpdate();
			} catch (Exception e) {
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public Collection getPrices() {
		PriceClass price;
		ArrayList arraylist = new ArrayList();
		try {
			Statement statement = con.createStatement();
			ResultSet resultset = statement.executeQuery("select * from price");

			while (resultset.next()) {
				price = new PriceClass();
				int id = resultset.getInt(1);
				String scope = resultset.getString(2);

				price.setId(id);
				price.setScope(scope);
				arraylist.add(price);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arraylist;
	}

	public void deletePrice(String s) {
		try {
			int i = Integer.parseInt(s);
			Statement statement = con.createStatement();
			statement.execute("delete from price where id='" + i + "'");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
